//
//  HMHomeViewModel.swift
//  sina
//
//  Created by Kent on 16/9/27.
//  Copyright © 2016年 Ming. All rights reserved.
//

import UIKit

class HMHomeViewModel: NSObject {
    
    var statusViewModelArray: [HMStatusViewModel] = [HMStatusViewModel]()
    
    func loadData(finished: @escaping (Bool) -> ()) {
        
        //2.00c1qxIG5LyW_B3f017410a30AZKdh
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        let parameter = ["access_token" : HMUserAccountViewModel.sharedAccountViewModel.userAccount?.access_token ?? ""]
        
        HMNetworkTools.sharedTool.request(method: .GET, urlString: urlString, parameters: parameter) { (responseObject, error) in
            
            if error != nil {
                finished(false)
                return
            }
            
            let dict = responseObject as! [String : Any]
            
            guard let sArray = dict["statuses"] as? [[String : Any]] else {
                finished(false)
                return
            }
            
            for item in sArray {
                let viewModel = HMStatusViewModel()
                let s = HMStatus()
                
                s.yy_modelSet(with: item)
                viewModel.status = s
                
                
                self.statusViewModelArray.append(viewModel)
            }
            
//            print(self.statusViewModelArray)

//            for item in self.statusViewModelArray {
//                print(item.status?.user?.name,item.status?.created_at)
//            }
            
            finished(true)
            
        }
        
    }

}
